package com.api.ld.web;

import com.engine.ld.functions.sap.gjly.ZMM_TOOLRECIPIENTS_IMPProxy;
import com.engine.ld.optimize.Gjly;
import com.engine.ld.utils.DealXml;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.rmi.RemoteException;
import java.util.Map;

/**
 * @author ZhangLei
 * @date 2021/7/1
 * 工具领用
 */
public class GjlyAction implements Action {
    private static final BaseBean bb = new BaseBean();

    @Override
    public String execute(RequestInfo requestInfo) {
        return GjlyHandle(requestInfo.getRequestid(),requestInfo);
    }

    private String GjlyHandle(String requestid,RequestInfo requestInfo) {
        String back = Action.FAILURE_AND_CONTINUE;
        bb.writeLog("step2--------------");
        ZMM_TOOLRECIPIENTS_IMPProxy pp = new ZMM_TOOLRECIPIENTS_IMPProxy();
        try {
            String SAPBackXml = pp.getZMM_TOOLRECIPIENTS_IMP_PortType().ZMM_TOOLRECIPIENTS_IMP(new Gjly().GjlyXml(requestid,requestInfo));
//            String SAPBackXml = pp.getZMM_TOOLRECIPIENTS_IMP_PortType().ZMM_TOOLRECIPIENTS_IMP(tureXml);
//            bb.writeLog("传给SAP接口的参数是："+tureXml);
            bb.writeLog("SAP的返回结果是：" + SAPBackXml);
            Map<String, Object> sapBackMap = new DealXml().XmlResultMap(SAPBackXml);
            bb.writeLog("step6---"+sapBackMap);
            String type = sapBackMap.get("TYPE").toString();
            String message = sapBackMap.get("MESSAGE").toString();
            message = message.contains("'")?message.replace("'","''"):message;
            bb.writeLog("解析后，type:"+type+";message:"+message);

            boolean b = saptodatabase(type, message, requestid);
            bb.writeLog("SAP返回结果添加到数据库："+b);
            if ("S".equals(type)){
                back = Action.SUCCESS;
            }else {
                back = Action.FAILURE_AND_CONTINUE;
                bb.writeLog("SAP返回结果为E,错误信息为："+message);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        bb.writeLog("返回的结果是："+back);
        return back;
    }


    /*将sap返回的结果存入数据库*/
    private boolean saptodatabase(String type, String message,String requestid){
        String sql = "UPDATE formtable_main_152 SET TYPE='"+type+"',MESSAGE='"+message+"' WHERE requestId="+requestid;
        RecordSet rs = new RecordSet();
        return rs.executeUpdate(sql);
    }
}
